package aiball

import org.springframework.dao.DataIntegrityViolationException
import grails.plugins.springsecurity.Secured;

class PlayerStatsController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [playerStatsInstanceList: PlayerStats.list(params), playerStatsInstanceTotal: PlayerStats.count()]
    }
	@Secured(['ROLE_ADMIN'])
    def create() {
        [playerStatsInstance: new PlayerStats(params)]
    }
	@Secured(['ROLE_USER', 'ROLE_ADMIN'])
    def save() {
        def playerStatsInstance = new PlayerStats(params)
        if (!playerStatsInstance.save(flush: true)) {
            render(view: "create", model: [playerStatsInstance: playerStatsInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'playerStats.label', default: 'PlayerStats'), playerStatsInstance.id])
        redirect(action: "show", id: playerStatsInstance.id)
    }

    def show() {
        def playerStatsInstance = PlayerStats.get(params.id)
        if (!playerStatsInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'playerStats.label', default: 'PlayerStats'), params.id])
            redirect(action: "list")
            return
        }

        [playerStatsInstance: playerStatsInstance]
    }
	
	
	def calculatePortalStats()
	{
		def random = new Random();
		def sortKey = random.nextInt(5)+1;
		def label = "";
		switch (sortKey){
			case 1:
				label = "Tore"
				break;
			case 2:
				label = "gelbe Karten"
				break;
			case 3:
				label = "rote Karten"
				break;
			case 4:
				label = "gelaufene Felder"
				break;
			case 5:
				label = "Ballkontakte"
				break;
		}
		def top3 = Top3Stats.withCriteria{
					eq("label", label)
					and {
						order('value', 'desc')
					}
					}

		[top3Stats: top3, label: label]
	}

	
	@Secured(['ROLE_ADMIN'])
    def edit() {
        def playerStatsInstance = PlayerStats.get(params.id)
        if (!playerStatsInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'playerStats.label', default: 'PlayerStats'), params.id])
            redirect(action: "list")
            return
        }

        [playerStatsInstance: playerStatsInstance]
    }

	
	@Secured(['ROLE_ADMIN'])
    def update() {
        def playerStatsInstance = PlayerStats.get(params.id)
        if (!playerStatsInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'playerStats.label', default: 'PlayerStats'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (playerStatsInstance.version > version) {
                playerStatsInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'playerStats.label', default: 'PlayerStats')] as Object[],
                          "Another user has updated this PlayerStats while you were editing")
                render(view: "edit", model: [playerStatsInstance: playerStatsInstance])
                return
            }
        }

        playerStatsInstance.properties = params

        if (!playerStatsInstance.save(flush: true)) {
            render(view: "edit", model: [playerStatsInstance: playerStatsInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'playerStats.label', default: 'PlayerStats'), playerStatsInstance.id])
        redirect(action: "show", id: playerStatsInstance.id)
    }
	
	@Secured(['ROLE_ADMIN'])
    def delete() {
        def playerStatsInstance = PlayerStats.get(params.id)
        if (!playerStatsInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'playerStats.label', default: 'PlayerStats'), params.id])
            redirect(action: "list")
            return
        }

        try {
            playerStatsInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'playerStats.label', default: 'PlayerStats'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'playerStats.label', default: 'PlayerStats'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
	
}
